package pATT.DataSetsGenerator.dataSetsGenerator.generator.visual;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pATT.DataSetsGenerator.dataSetsGenerator.attribute.implement.Attribute;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Relation;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.RelationManager;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Support;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.DataManager;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.IData;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.RandomExponentialData;
import pATT.help.visual.HelpFrame;
import pATT.logger.Message;

@SuppressWarnings("serial")
public class RandomExponentialFrame extends RandomDistributionFrame {
	
	private JPanel jExponentialPanel1 = null;  //  @jve:decl-index=0:visual-constraint="10,54"
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JComboBox jComboBox = null;
	private JLabel jLabel = null;
	private JTextField jValuesTextField1 = null;
	
	public RandomExponentialFrame() throws HeadlessException {
		super();
		
		nameGenerator = "Random Exponential Generator";
		Relation relation = RelationManager.instance.getCurrentRelation();
		Vector<IData> vec = new Vector<IData>();
		vec = generateData(relation.getAtributes());
		manager = new DataManager(relation,vec,nameGenerator);
		
		super.setTitle("Relation Generator Rule - "+nameGenerator);
		super.jPanel.add(getJComboBox(), null);
		super.jPanel.add(getJPoissonPanel1(), null);
		super.jLabel1.setText(manager.getRelation().getName());
		Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension freim = this.getSize();
		if (pantalla.width < freim.width){
			freim.width = pantalla.width;
		}
		if (pantalla.height < freim.height) {
			freim.height = pantalla.height;
		}
		this.setLocation((pantalla.width-freim.width)/2,(pantalla.height-freim.height)/2);

	}
	
	private Vector<IData> generateData(Vector<Attribute> attributes) {
		Vector<IData> vector = new Vector<IData>();
		for (int i = 0; i < attributes.size(); i++) {
			vector.addElement(new RandomExponentialData(attributes.elementAt(i)));
		}
		return vector;
	}
	
	
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jComboBox.setSize(new java.awt.Dimension(111,24));
			jComboBox.setLocation(new java.awt.Point(213,10));
			jComboBox.setForeground(java.awt.Color.blue);
			Vector<Attribute> vecAttributes = manager.getRelation().getAtributes();
			for (int i = 0; i < vecAttributes.size(); i++) {
				if(vecAttributes.get(i).getType().equals("Integer") || vecAttributes.get(i).getType().equals("Real"))
					jComboBox.addItem(vecAttributes.get(i).getName());	
			}
			jComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(jComboBox.getSelectedIndex() != -1)
					if(jComboBox != null && jExponentialPanel1 != null){
						jExponentialPanel1.setVisible(true);
						jErrorLabel1.setVisible(false);
						int pos = jComboBox.getSelectedIndex();
						
						RandomExponentialData data = (RandomExponentialData)manager.getData(pos);
						int size = data.getAttribute().getValues().size();
						
						jValuesTextField1.setEditable(false);
						jValuesTextField1.setText(String.valueOf(size));
						if(data.getMedia() != -1)
							jTextField.setText(String.valueOf(data.getMedia()));
						else
							jTextField.setText("");
					}
				}
			});
		}
		return jComboBox;
	}
	
	
	private JPanel getJPoissonPanel1() {
		if (jExponentialPanel1 == null) {
			/*		jErrorLabel1 = new JLabel();
			 jErrorLabel1.setText("Please, complete all attribute's median");
			 jErrorLabel1.setLocation(new java.awt.Point(7,100));
			 jErrorLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			 jErrorLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.BOLD, 11));
			 jErrorLabel1.setForeground(java.awt.Color.red);
			 jErrorLabel1.setSize(new java.awt.Dimension(321,23));
			 jErrorLabel1.setVisible(false);
			 */
			jLabel = new JLabel();
			jLabel.setBounds(new java.awt.Rectangle(168,3,89,26));
			jLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel.setText("Attribute's values");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new java.awt.Rectangle(72,69,65,27));
			jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel4.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel4.setText("Median");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new java.awt.Rectangle(88,35,148,28));
			jLabel3.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.BOLD, 14));
			jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel3.setText("Exponential Values");
			jExponentialPanel1 = new JPanel();
			jExponentialPanel1.setLayout(null);
			jExponentialPanel1.setBounds(new java.awt.Rectangle(12,51,330,124));
			jExponentialPanel1.setBackground(new java.awt.Color(191,217,242));
			jExponentialPanel1.setVisible(false);
			jExponentialPanel1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jExponentialPanel1.add(jLabel3, null);
			jExponentialPanel1.add(jLabel4, null);
			jExponentialPanel1.add(getJTextField(), null);
			jExponentialPanel1.add(getJButton(), null);
			jExponentialPanel1.add(jLabel, null);
			jExponentialPanel1.add(getJValuesTextField1(), null);
			//	jExponentialPanel1.add(jErrorLabel1, null);
		}
		return jExponentialPanel1;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setLocation(new java.awt.Point(137,69));
			jTextField.setSize(new java.awt.Dimension(45,27));
		}
		return jTextField;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setLocation(new java.awt.Point(202,69));
			jButton.setText("Set");
			jButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton.setSize(new java.awt.Dimension(55,27));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(jComboBox != null){
						int pos = jComboBox.getSelectedIndex();
						//Attribute attr = randomGenerator.getRelation().getAtributes().get(pos);
						RandomExponentialData data = (RandomExponentialData)manager.getData(pos);
						
						int sizeNum = data.getAttribute().getValues().size();
						String size = String.valueOf(sizeNum);
						if(!Support.isDoublePositive(jTextField.getText()) || !Support.minusRealThan(jTextField.getText(), size))
							jTextField.setText("<complete>");
						else{
							double media = java.lang.Double.parseDouble(jTextField.getText());
							//almacenar la media con el atributo
							data.setMedia(media);
							
							jExponentialPanel1.setVisible(false);
							jErrorLabel1.setVisible(false);
						}
					}
					
				}
			});
		}
		return jButton;
	}
	
	/**
	 * This method initializes jValuesTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJValuesTextField1() {
		if (jValuesTextField1 == null) {
			jValuesTextField1 = new JTextField();
			jValuesTextField1.setEditable(true);
			jValuesTextField1.setSize(new java.awt.Dimension(64,26));
			jValuesTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
			jValuesTextField1.setLocation(new java.awt.Point(257,3));
		}
		return jValuesTextField1;
	}

	@Override
	protected void callHelp() {
		new HelpFrame(Message.TITLE_RANDOM_EXP,Message.BODY_RANDOM_EXP).show();
		
	}
}
